package com.yuezhi.mylauncher.dialogs;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuezhi.mylauncher.customviews.InputPwdView_Pwd;
import com.yuezhi.mylauncher.R;
import com.yuezhi.mylauncher.base.BaseDialog;
import com.yuezhi.mylauncher.utils.AESUtil;
import com.yuezhi.mylauncher.utils.AppUtils;
import com.yuezhi.mylauncher.utils.ToastUtil;
import com.yuezhi.mylauncher.utils.ZXingUtils;

public class QuitDialog extends BaseDialog {

    private final Context context;
    private InputPwdView_Pwd myPwdInput;
    private GridView listView;
    private String mPassWord;
    private String code;

    public QuitDialog(Context context) {
        super(context, R.style.loading_dialog);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 指定布局
        this.setContentView(R.layout.quit_dialog);
        myPwdInput = findViewById(R.id.MyPwdInput_inputpwd);
        listView = findViewById(R.id.MyPwdInput_list);
        ImageView ivQrcode = findViewById(R.id.iv_qrcode);
        initSoft();
        code = AppUtils.getCode(6);
        String aesEncrypt = AESUtil.aesEncrypt(code, AESUtil.VIPARA);
        Bitmap qrImage = ZXingUtils.createQRImage(aesEncrypt, 800, 800);
        ivQrcode.setImageBitmap(qrImage);

        TextView tv_no = findViewById(R.id.tv_no);
        tv_no.setOnClickListener(v -> QuitDialog.this.dismiss());

        TextView tv_app_setting = findViewById(R.id.tv_app_setting);
        tv_app_setting.setOnClickListener(v -> {
            if (TextUtils.isEmpty(code)) {
                ToastUtil.showMessage("密码生成失败，请重试！");
                return;
            }
            if (TextUtils.isEmpty(mPassWord)) {
                ToastUtil.showMessage("请输入密码");
                return;
            }
            if (mPassWord.equals(code)) {
                AppUtils.startSettingActivity();
                QuitDialog.this.dismiss();
            } else {
                ToastUtil.showMessage("密码输入错误");
                myPwdInput.reSetView();
                mPassWord = "";
            }
        });

        Window dialogWindow = this.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams p = dialogWindow.getAttributes();
        dialogWindow.setAttributes(p);
        this.setCancelable(false);
    }

    private void initSoft() {
        listView.setAdapter(new MAdapter());
        listView.setOnItemClickListener((adapterView, view, i, l) -> {
            if (i == 11) {
                myPwdInput.deletePwd();
            } else if (i == 10) {
                myPwdInput.addPwd("0");
            } else if (i == 9) {
                Log.e("TAG", "initSoft: 不输出任何内容");
            } else {
                myPwdInput.addPwd(String.valueOf(++i));
            }

        });
        myPwdInput.setOnFinishListener(pwd -> mPassWord = pwd);
    }

    class MAdapter extends BaseAdapter {


        @Override
        public int getCount() {
            return 12;
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            @SuppressLint("ViewHolder") View cView = LayoutInflater.from(context).inflate(R.layout.myinputviewlist_item, viewGroup, false);
            TextView tv = cView.findViewById(R.id.myInputPwdListItem_tv);
            if (i == 9) {
                tv.setVisibility(View.GONE);
                cView.findViewById(R.id.myInputPwdListItem_delete).setVisibility(View.GONE);
            } else if (i == 10) {
                cView.findViewById(R.id.myInputPwdListItem_delete).setVisibility(View.GONE);
                tv.setVisibility(View.VISIBLE);
                tv.setText("0");
            } else if (i == 11) {
                cView.findViewById(R.id.myInputPwdListItem_delete).setVisibility(View.VISIBLE);
                tv.setVisibility(View.GONE);
            } else {
                tv.setText(String.format("%s", i + 1));
                cView.findViewById(R.id.myInputPwdListItem_delete).setVisibility(View.GONE);
                tv.setVisibility(View.VISIBLE);
            }

            return cView;
        }
    }
}
